package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPublishservicePriceListBinding;
import com.sgb.kjwl.view.ui.adapter.enterprise.PublishServicePriceListAdpaterNew;
import com.sgb.kjwl.viewmodel.enterprise.PublishServicePriceListViewModel;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 发布服务添加分类价格
 */
public class PublishServicePriceListActivity extends AppActivity {

    private ActivityPublishservicePriceListBinding mBinding;
    private PublishServicePriceListViewModel mViewModel;
    private PublishServicePriceListAdpaterNew mAdpater;
    private LinearLayoutManager manager;


    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(PublishServicePriceListViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_publishservice_price_list);
        mBinding.setVariable(BR.model, mViewModel);
        mBinding.setLifecycleOwner(this);

        mViewModel.listdata = (List<Map<String, Object>>) getIntent().getSerializableExtra("data");

        showSaveBtn();
        initRecyclerView();
        mBinding.tvSaveBtn.setOnClickListener(view -> {

            Log.e("message", new Gson().toJson(mViewModel.listdata));

            BigDecimal maxvalue = null;
            BigDecimal minvalue = null;

            for (int i = 0; i < mViewModel.listdata.size(); i++) {
                if(mViewModel.listdata.get(i).get("price")==null|| TextUtils.isEmpty(mViewModel.listdata.get(i).get("price").toString())){
                    MToast.showToast(PublishServicePriceListActivity.this, "请输入价格");
//                    manager.scrollToPositionWithOffset(i,0);
                    return;
                }
                if(!"-1".equals(mViewModel.listdata.get(i).get("price").toString())&&Double.parseDouble(mViewModel.listdata.get(i).get("price").toString())==0){
                    MToast.showToast(PublishServicePriceListActivity.this, "服务收费需大于0");
//                    manager.scrollToPositionWithOffset(i,0);
                    return;
                }
                if(!"-1".equals(mViewModel.listdata.get(i).get("price").toString())){
                    if(maxvalue==null&&minvalue==null){
                        maxvalue = new BigDecimal(mViewModel.listdata.get(i).get("price").toString());
                        minvalue = new BigDecimal(mViewModel.listdata.get(i).get("price").toString());
                    }else{

                        if( new BigDecimal(mViewModel.listdata.get(i).get("price").toString()).doubleValue()>maxvalue.doubleValue()){
                            maxvalue = new BigDecimal(mViewModel.listdata.get(i).get("price").toString());
                        }

                        if(new BigDecimal(mViewModel.listdata.get(i).get("price").toString()).doubleValue()<minvalue.doubleValue()){
                            minvalue = new BigDecimal(mViewModel.listdata.get(i).get("price").toString());
                        }
                    }
                    if(mViewModel.listdata.get(i).get("price").toString().endsWith(".")){
                        mViewModel.listdata.get(i).put("price",mViewModel.listdata.get(i).get("price").toString()+"00");
                    }

                }
            }

            Intent mIntent = new Intent();
            Bundle mBundle = new Bundle();
            mBundle.putSerializable("data", (Serializable) mViewModel.listdata);
            mBundle.putString("maxprice",maxvalue==null?"-1": maxvalue.stripTrailingZeros().toPlainString());
            mBundle.putString("mminprice",maxvalue==null?"-1":minvalue.stripTrailingZeros().toPlainString());
            mIntent.putExtras(mBundle);
            setResult(PublishServiceActivity.RESULT_OK, mIntent);
            finish();

        });

    }


    private void initRecyclerView() {
        manager = new LinearLayoutManager(this);
        mAdpater = new PublishServicePriceListAdpaterNew(this, mViewModel.listdata);
        mBinding.recyclerViewTrand.setLayoutManager(manager);
        mBinding.recyclerViewTrand.setAdapter(mAdpater);

    }

    private void showSaveBtn() {
        mBinding.tvSaveBtn.setVisibility(mViewModel.listdata.size() > 0 ? View.VISIBLE : View.GONE);
        mBinding.recyclerViewTrand.setVisibility(mViewModel.listdata.size() > 0 ? View.VISIBLE : View.GONE);
        mBinding.include.setVisibility(mViewModel.listdata.size() > 0 ? View.GONE : View.VISIBLE);
    }
}
